#include "HelloWorldScene.h"
#include "kkGame/JobManager.h"

USING_NS_CC;
USING_NS_CC_EXT;

void HelloWorld::onInitEventHandlers() {

	registerEventHandler("exitApp", callfuncND_selector(HelloWorld::exitApp));
	registerEventHandler("show", callfuncND_selector(HelloWorld::showTarget));
	registerEventHandler("hide", callfuncND_selector(HelloWorld::hideTarget));
	registerEventHandler("focus", callfuncND_selector(HelloWorld::showFocus));
	registerEventHandler("unfocus", callfuncND_selector(HelloWorld::loseFocus));
	registerEventHandler("select", callfuncND_selector(HelloWorld::setSelected));
	registerEventHandler("unselect",
			callfuncND_selector(HelloWorld::setUnselected));
	registerEventHandler("fadeIn",
			callfuncND_selector(HelloWorld::fadeInTarget));
	registerEventHandler("fadeOut",
			callfuncND_selector(HelloWorld::fadeOutTarget));

	registerEventHandler("setProperty",
			callfuncND_selector(HelloWorld::setTargetProperty));

	registerEventHandler("onMove", callfuncND_selector(HelloWorld::onMove));
}
CCScene* HelloWorld::scene() {

	HelloWorld *director = HelloWorld::create();

	director->onInitEventHandlers();
	if (director) {
		return director->getCurrentScene();
	}

	CC_SAFE_DELETE(director);

	// return the scene
	return NULL;
}

// on "init" you need to initialize your instance
bool HelloWorld::init() {
	//////////////////////////////
	// 1. super init first
	if (!KKDirector::init()) {
		return false;
	}

	loadProject("project.xml");
	//
	//	KKLayer * bg = dynamic_cast<KKLayer*> (this->getCurrentScene()->nodeNamed(
	//			"bg"));
	//	int count = bg->getChildrenCount();
	//
	//	for (int i = 0; i < count; ++i) {
	//		CCObject * obj = bg->getChildren()->objectAtIndex(i);
	//		CCNode* node = dynamic_cast<CCNode*> (obj);
	//		if (node) {
	//			CCLog("node order:%d,node tag:%d,node width:%f height:%f\n",
	//					node->getZOrder(), node->getTag(),
	//					node->getContentSize().width, node->getContentSize().height);
	//
	//		}
	//	}
	//
	//	KKSprite * bao =
	//			dynamic_cast<KKSprite*> (this->getCurrentScene()->nodeNamed("bao"));
	//
	//	CCSprite* sprite = dynamic_cast<CCSprite*> (bao);
	//
	//	CCLog("sprite parent:%d,sprite tag=%d\n", sprite->getParent()->getTag(),
	//			sprite->getTag());
	//
	//	return true;

	KKLayer* layer = KKLayer::layerWithWidthHeight(700, 700);

	CCSprite* pFirst = CCSprite::create("HelloWorld.png");
	pFirst->setPosition(ccp(960, 600));

	CCSprite* lan = CCSprite::create("lan.png");
	lan->setPosition(ccp(960, 800));

	layer->addChild(lan, 1);
	layer->addChild(pFirst, -201);

	KKScene* scene = KKDirector::shareDirector()->getCurrentScene();
	scene->addChild(layer);

	CCLog("name:%s\n", scene->getName().c_str());

	//	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	//	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	//	CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png",
	//			"CloseSelected.png", this,
	//			menu_selector(HelloWorld::menuCloseCallback));
	//
	//	pCloseItem->setPosition(
	//			ccp(
	//					origin.x + visibleSize.width
	//							- pCloseItem->getContentSize().width / 2,
	//					origin.y + pCloseItem->getContentSize().height / 2));
	//
	//	CCLog("pCloseItem:x=%f,pCloseItem.y=%f\n", pCloseItem->getPositionX(),
	//			pCloseItem->getPositionY());
	//
	//	// create menu, it's an autorelease object
	//	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	//	pMenu->setPosition(CCPointZero);
	//
	//	KKLayer* bg = dynamic_cast<KKLayer*> (this->getCurrentScene()->nodeNamed(
	//			"bg"));
	//	bg->addChild(pMenu, 1);
	//	KKSprite* lan = dynamic_cast<KKSprite*> (bg->nodeNamed("lan"));
	//	lan->setIsVisible(true);
	//	lan->loadNode();
	//
	//	CCLog("lan x=%f,y=%f\n", lan->getPositionX(), lan->getPositionY());
	//	CCLog("menu x=%f,y=%f\n", pCloseItem->getPositionX(),
	//			pCloseItem->getPositionY());

	//lan->setPosition(ccp(200, 200));

	//KKSprite* lansprite = KKSprite::spriteWithXmlFile("lan.sprite", bg);

	//bg->addChild(lansprite);

	//lansprite->setPosition(ccp(100, 100));

	//KKSprite* lansprite2 = KKSprite::spriteWithFile("lan.png");

	//bg->addChild(lansprite2);

	//lansprite2->setPosition(ccp(100, 100));

	//	return true;
	//	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	//	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	//
	//	/////////////////////////////
	//	// 2. add a menu item with "X" image, which is clicked to quit the program
	//	//    you may modify it.
	//
	//	// add a "close" icon to exit the progress. it's an autorelease object
	//	CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png",
	//			"CloseSelected.png", this,
	//			menu_selector(HelloWorld::menuCloseCallback));
	//
	//	pCloseItem->setPosition(ccp(
	//			origin.x + visibleSize.width
	//			- pCloseItem->getContentSize().width / 2,
	//			origin.y + pCloseItem->getContentSize().height / 2));
	//
	//	// create menu, it's an autorelease object
	//	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	//	pMenu->setPosition(CCPointZero);
	//	this->addChild(pMenu, 1);
	//
	//	/////////////////////////////
	//	// 3. add your codes below...
	//
	//	// add a label shows "Hello World"
	//	// create and initialize a label
	//
	//	CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
	//
	//	// position the label on the center of the screen
	//	pLabel->setPosition(ccp(
	//			origin.x + visibleSize.width / 2,
	//			origin.y + visibleSize.height
	//			- pLabel->getContentSize().height));
	//
	//	// add the label as a child to this layer
	//	this->addChild(pLabel, 1);
	//
	//	// add "HelloWorld" splash screen"
	//	CCSprite* pSprite = CCSprite::create("HelloWorld.png");
	//
	//	// position the sprite on the center of the screen
	//	pSprite->setPosition(ccp(visibleSize.width / 2 + origin.x,
	//			visibleSize.height / 2 + origin.y));
	//
	//	// add the sprite as a child to this layer
	//	this->addChild(pSprite, 0);
	//
	//	//	std::string path = "http://127.0.0.1:8000/jobs_list/";
	//	//	KKHttpUtils* h = KKHttpUtils::create();
	//	//	h->getText(path.c_str(), "test", this, NULL);
	//
	//	JobManager::sharedController<JobManager>()->listJobs(this,
	//			model_selector(HelloWorld::initJobList));
	return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender) {
	CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}

void HelloWorld::initJobList(CCObject* obj, void* args) {
	KKModelList* jobs = dynamic_cast<KKModelList*> (obj);
	KKModel* model = NULL;
	KKMODELLIST_FOREACH(jobs,model) {
		CCLog("good:pk:%s ", model->getstringProperty("pk").c_str());
		KKModel* fields = model->getForeignValue("fields");
		CCLog("job_title:%s,job_des:%s,pub_date=%s\n",
				fields->getstringProperty("job_title").c_str(),
				fields->getstringProperty("job_description").c_str(),
				fields->getstringProperty("pub_date").c_str());
	}

}

void HelloWorld::onMove(CCNode* pSender, void* args) {
	CCPoint point = pSender->getPosition();
	CCLog("x=%f,y=%f\n", point.x, point.y);
}
